
package com.sxtyxt.online;

import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.sxtyxt.online.file.ActFileDetail;
import com.sxtyxt.online.me.ActMe;


/**
 * 主界面
 *
 * @author 马海明
 * @created 马海明 2015年7月29日
 */

public class ActMains extends ActBase implements OnClickListener {

    private TextView txtSendFile, txtReceiverFile, txtDataStat, txtAddrList, txtPersonal;
    private EditText etxtLogisticsId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    protected void initUI() {
        super.initUI();
        txtSendFile = (TextView) findViewById(R.id.txt_send_file);
        txtSendFile.setOnClickListener(this);
        txtReceiverFile = (TextView) findViewById(R.id.txt_receive_file);
        txtReceiverFile.setOnClickListener(this);
        txtDataStat = (TextView) findViewById(R.id.txt_data_stat);
        txtAddrList = (TextView) findViewById(R.id.txt_addr_list);
        txtPersonal = (TextView) findViewById(R.id.txt_personal);
        txtPersonal.setOnClickListener(this);

        etxtLogisticsId = (EditText)findViewById(R.id.etxt_logisticsId);
        etxtLogisticsId.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Drawable drawable = etxtLogisticsId.getCompoundDrawables()[2];
                //如果右边没有图片，不再处理
                if (drawable == null)
                    return false;
                //如果不是按下事件，不再处理
                if (event.getAction() != MotionEvent.ACTION_UP)
                    return false;

                if (event.getX() > etxtLogisticsId.getWidth()
                        - etxtLogisticsId.getPaddingRight()
                        - drawable.getIntrinsicWidth()){
                    transfer(ActFileDetail.class);
                }
                return false;
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("ActMains----------", "onResume");
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_receive_file:
                transfer(ActReceiveFiles.class);
                break;
            case R.id.txt_send_file:
                transfer(ActSendFiles.class);
                break;
            case R.id.btn_get_code:

                break;
            case R.id.txt_personal:
                transfer(ActMe.class);
                break;
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    boolean isExit = false;

    public void exit() {
        if (!isExit) {
            isExit = true;
            doToast("再按一次退出程序");
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            SystemEnv.systemOut();
        }
    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }

    };


}
